<html>
<head></head>
<body>

<?php

// 2010-12-29

require_once dirname(__FILE__) . '/common.inc.php' ;

echo "<h1>".basename(__FILE__)."</h1>" ;

$schema =
"
  show:
    + show_form
    -> ok     check
    
  check:
    + check_form
    -> succes send
    -> errors show
    
  send:
    + send_form
    + show_result
    + show_fresh_demo
    -> ok     show
    
" ;

$sm = new phpSmart();
$sm->load($schema);

//-------------------------------------------------------------------------

function show_form($obj,&$pass) {
  
  // reset fresh() status
  $obj->reset() ;
  
  $ok_event_hidden = $obj->getNextEvent('ok','form') ;
  
  if ( empty($pass) ) $pass = array() ;
  
  echo "<form method=\"post\">";
  echo $ok_event_hidden;
  echo "First Name <input type=\"text\" name=\"fname\" value=\"".htmlspecialchars($pass['values']['fname'])."\"> {$pass['errors']['fname']}<br />";
  echo "Last Name <input type=\"text\" name=\"lname\" value=\"".htmlspecialchars($pass['values']['lname'])."\"> {$pass['errors']['lname']}<br />";
  echo "<input type=\"submit\" name=\"Submit\">";
  echo "</form>";

}

function show_fresh_demo($obj,&$pass) {
  
  if ( true === $obj->fresh() ) {
    echo "<p style='color: green'>"
       . "Notice: This page is fresh, no browser refresh, no back button clicked<br />"
       . "or no session available. Refresh page for test..."
       . "</p>" ;
  } elseif ( false === $obj->fresh() ) {
    echo "<p style='color: red'>"
       . "Notice: This page is NOT fresh, browser refresh or back button clicked."
       . "</p>" ;
  }
  
}

function check_form($obj,&$pass) {
  
  $errors = array() ;
  
  $formvars['values'] = array_map('stripslashes',$_POST) ;
  
  if ( empty($formvars['values']['fname']) ) $errors['fname'] = 'field required' ;
  if ( empty($formvars['values']['lname']) ) $errors['lname'] = 'field required' ;

  
  if ( empty($errors) ) {
    
    // go to send_form()
    
    $obj->setNextEvent('succes',$formvars);
    
  } else {
    
    // pass errors to next event "errors" (contact form)
    
    $formvars['errors'] = $errors ;
    $obj->setNextEvent('errors',$formvars);
    
  }
}

function send_form($obj,&$pass) {
  
  $to        = "to@domain.com" ; // CHANGE MAIL ADRESS FOR TEST !!!
  $subject   = "Contact form" ;
  $additional_headers    = "From: {$to}" ;
  $additional_parameters = "-f{$to}" ;
  
  $message = "First Name: " . htmlspecialchars($pass['values']['fname']) . "\n"
           . "Last Name: "  . htmlspecialchars($pass['values']['lname']) . "\n" ;
  
  // Test fresh() return false if refresh browser or back.
  // Fresh() return already true if session unavailable.
  
  if ( $obj->fresh() ) {
  
    if ( "to@domain.com" == $to ) {
      
      // just for test
      $sendmail_result = true ;
      
    } else {
      
      $sendmail_result = false ;
      if ( mail ($to,$subject,$message,$additional_headers,$additional_parameters) ) {
        $sendmail_result = true ;
      }
      
    }
  
  } else {
    
    // error if refresh page
    $sendmail_result = false ;
    
  } // end test $obj->fresh()
  
  $pass['to'] = $to ;
  $pass['sendmail_result'] = $sendmail_result ;

}

function show_result($obj,&$pass) {
  
  $ok_event_link = $obj->getNextEvent('ok','url') ;
  
  if ( true == $pass['sendmail_result'] ) {
    echo "successful form sended to {$pass['to']}.<br />Thanks<br /><br />" ;
  } else {
    echo "bad form sending to {$pass['to']}.<br />Thanks<br /><br />" ;
  }
  echo "<a href=\"?{$ok_event_link}\">go to contact form</a><br />" ;
  
}

?>

<hr />
<a target="_blank" href="show_source.php?src=<?php echo basename(__FILE__); ?>">Show <?php echo basename(__FILE__); ?> source</a>

</body>
</html>